<template>
    <g-scrollbar class="dashboard">
        <div class="dashboard-content">
            <div class="dashboard-item">
                <div class="dashboard-item-content">
                    <div class="dashboard-item-resources-wrapper">
                        <div class="dashboard-item-resources"
                            :class="getState( storageQuota, storageConsume )">
                            <p class="resources-name">数据存储</p>
                            <p class="resources-statistics">
                                <!-- <span class="resources-used">{{ storageConsume }}</span>/{{ Number( storageQuota ) === -1 ? '无限制(M)' : storageQuota + 'M' }}</p> -->
                                <span class="resources-used">{{ (storageConsume > 1024) ? (storageConsume/1024).toFixed( 1 ) : storageConsume }}</span>{{storageConsume > 1024 ? 'G' : 'Mb'}}
                            </p>
                            <div class="progress-bar">
                                <template v-if="Number(storageQuota) != -1">
                                    <div class="progress-bar-container">
                                        <div class="progress-bar-content" :style="{width: (storageConsume/storageQuota) * 100 + '%'}"></div>
                                    </div>
                                    <div class="progress-bar-text">{{storageQuota > 1024 ? (Number(storageQuota)/1024).toFixed( 1 )+'G' : Number(storageQuota).toFixed(0)+'Mb' }}</div>
                                </template>
                                <div v-else>
                                    无限制
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="dashboard-item-resources-wrapper">
                        <div class="dashboard-item-resources"
                            :class="getState( grQuota, grConsume )">
                            <p class="resources-name">地址匹配</p>
                            <p class="resources-statistics">
                                <!-- <span class="resources-used">{{ grConsume }}</span>/{{ Number( grQuota ) === -1 ? '无限制' : grQuota }}(次)</p> -->
                                <span class="resources-used">{{ grConsume }}</span>次
                            </p>
                            <div class="progress-bar">
                                <template v-if="Number(grQuota) != -1">
                                    <div class="progress-bar-container">
                                        <div class="progress-bar-content" :style="{width: (grConsume/Number(grQuota)) * 100 + '%'}"></div>
                                    </div>
                                    <div class="progress-bar-text">{{Number(grQuota).toFixed(0)}}次</div>
                                </template>
                                <div v-else>
                                    无限制
                                </div>
                            </div>
                            
                        </div>
                    </div>
                    <!-- <div class="dashboard-item-resources-wrapper">
                        <div class="dashboard-item-resources"
                            :class="getState( apiQuota, apiConsume )">
                            <p class="resources-name">服务接口调用</p>
                            <p class="resources-statistics">
                                <span class="resources-used">{{ apiConsume }}</span>/{{ Number( apiQuota ) === -1 ? '无限制' : apiQuota }}(次)</p>
                        </div>
                    </div> -->
                </div>
                <div class="split-line"></div>
                <div class="dashboard-item-content">
                    <div class="dashboard-item-resources-wrapper">
                        <div class="dashboard-item-resources">
                            <p class="resources-name">数据数量</p>
                            <p class="resources-statistics">
                                <span class="resources-count">{{ dataCount }}</span>个
                            </p>
                        </div>
                    </div>
                    <div class="dashboard-item-resources-wrapper">
                        <div class="dashboard-item-resources">
                            <p class="resources-name">成果访问量</p>
                            <p class="resources-statistics">
                                <span class="resources-count">{{ visitCount }}</span>次
                            </p>
                        </div>
                    </div>
                </div>
            </div>
            <div class="dashboard-item">
                <p>成果访问量TOP5</p>
                <!-- <dashboard-resource></dashboard-resource> -->
                <ul class="top5-list" v-if="visitTop5.length > 0">
                    <li class="top5-li header">
                        <div class="top5-no">排名</div>
                        <div class="top5-title">名称</div>
                        <div class="top5-visit">访问次数</div>
                    </li>
                    <li class="top5-li" v-for="(item, index) in visitTop5">
                        <div class="top5-no">{{index + 1}}</div>
                        <div class="top5-title">
                            <a target="_blank" :href="item.url">
                                <div class="resource-img-container">
                                    <!-- <img @load="imgLoaded" :src="`/s/mapviz/${item.uid}/thumb`"> -->
                                    <img :src="`${appNameMapping[item.type]['rootPath']}${item.uid}/thumb`" :onerror="imageError">
                                </div>
                                {{item.title}}
                            </a>
                        </div>
                        <div class="top5-visit">{{item.visit}}</div>
                    </li>
                </ul>

                <div class="top5-empty" v-else>
                    <img src="../../../static/img/no-data.png">
                    <div class="no-data-tip">您尚未分享成果</div>
                </div>
            </div>
        </div>
    </g-scrollbar>
</template>

<script>

import Bus from '@/utils/event_bus'
import { dashboard as dashboardAPI } from '@/api'
import GScrollbar from '@/components/common/g-scrollbar'
import GButton from '@/components/common/g-button'
// import DashboardResource from './dashboard-resource'
import { animate } from '@/utils/animate'
import { appNameMapping, defaultImg } from '@/config/config'

const numberAnimate = ( start, end, onUpdate, delay = 0 ) => {
    start = Number( start );
    end = Number( end );
    let duration = 1000;

    if ( start === -1 || end === -1 ) {
        duration = 0;
    }

    setTimeout( () => {
        animate( { number: start }, { number: end }, duration, {
            onUpdate
        } );
    }, delay )
}

export default {
    name: 'Source',
    data () {
        return {
            storageQuota: 0,
            storageConsume: 0,
            // apiQuota: 0,
            // apiConsume: 0,
            grQuota: 0,
            grConsume: 0,
            dataCount: 0,
            visitCount: 0,
            visitTop5: [],
            appNameMapping: appNameMapping,
            imageError: `this.src='${defaultImg}'`
        }
    },
    activated () {
        this.$emit( 'on-view-load' );
    },
    beforeRouteLeave ( to, from, next ) {
        // this.$destroy();
        next();
    },
    mounted () {
        const vm = this;
        Bus.$emit('on-submenu-change', '');

        dashboardAPI.getQuota().then( res => {
            if ( res.data.code === 0 ) {
                let data = res.data.data;

                numberAnimate( 0, data.storageQuota, function() {
                    vm.storageQuota = this.number.toFixed( 1 );
                } )

                numberAnimate( 0, data.storageConsume, function() {
                    vm.storageConsume = this.number.toFixed( 1 );
                }, 16 )

                // numberAnimate( 0, data.apiQuota, function() {
                //     vm.apiQuota = this.number.toFixed( 0 );
                // } )

                // numberAnimate( 0, data.apiConsume, function() {
                //     vm.apiConsume = this.number.toFixed( 0 );
                // }, 16 )

                numberAnimate( 0, data.grQuota, function() {
                    vm.grQuota = this.number.toFixed( 0 );
                } )

                numberAnimate( 0, data.grConsume, function() {
                    vm.grConsume = this.number.toFixed( 0 );
                }, 16 )

                if ( this.getState( data.storageQuota, data.storageConsume ) === 'danger' ) {
                    this.warning( '数据存储已达上限',
                        '您当前方案的数据存储已达到配额上限，无法继续使用上传数据等功能。请进入个人中心升级您的方案。'  )
                }

                if ( this.getState( data.grQuota, data.grConsume ) === 'danger' ) {
                    this.warning( '数据空间化次数已达上限',
                        '您当前方案的数据空间化次数已达到配额上限，无法继续使用数据空间化功能。请进入个人中心升级您的方案。'  )
                }

                if ( this.getState( data.apiQuota, data.apiConsume ) === 'danger' ) {
                    this.warning( '服务接口调用次数已达上限',
                        '您当前方案的服务接口调用次数已达到配额上限，无法继续使用服务接口。请进入个人中心升级您的方案。'  )
                }

            }
        } ).catch( err => console.error( err ) );


        dashboardAPI.getStatistic().then( res => {

            if (res.data.code === 0) {

                let data = res.data.data;

                numberAnimate( 0, data.dataCount, function() {
                    vm.dataCount = this.number.toFixed( 0 );
                }, 16 )

                numberAnimate( 0, data.visitCount, function() {
                    vm.visitCount = this.number.toFixed( 0 );
                }, 16 )

                this.visitTop5 = data.visitTop5;
            }
        } ).catch( err => console.error(err));
    },
    methods: {
        getState ( quota, consume ) {

            quota = Number( quota )
            consume = Number( consume )

            if ( consume === 0 || quota === -1 ) return 'normal'

            if ( quota === consume ) return 'danger'

            if ( consume / quota >= 0.9 ) return 'warning'

            return 'normal'
        },
        warning ( title, message, duration = 50000 ) {

            // 必须在setTimeout中调用，否则会在连续弹出时因为无法计算高度而重叠
            setTimeout( () => {
                this.$notify.warning( {
                    title,
                    message,
                    duration
                } );
            }, 100 )
        }
    },
    components: {
        GButton,
        // DashboardResource,
        GScrollbar
    }
}
</script>

<style lang="scss" scoped>
@import 'common';
.dashboard {
    width: 100%;
    height: 100%;

    .dashboard-content {
        padding: 40px 40px 0;
        min-width: 882px;
        max-width: 1200px;

        .dashboard-item {
            margin-bottom: 30px;
            padding-bottom: 50px;
            overflow: hidden;
            position: relative;

            &:last-child {
                border: 0;
            }

            .split-line {
                float: left;
                height: 80px;
                width: 0;
                margin-top: 18px;
                margin-bottom: 18px;
                margin-left: 5%;
                margin-right: 5%;
                border: 0.5px solid #ccc;
            }

            > h3 {
                font-weight: 400;
                margin-bottom: 20px;

                > label {
                    font-size: 12px;
                    padding: 2px 6px;
                    margin-left: 20px;
                    color: $white;
                    background-color: $darkPrimary;
                    border-radius: 4px;
                }
            }

            .dashboard-item-content {

                width: 45%;
                min-width: 380px;
                float: left;

                &::after {
                    @include clearfix();
                }
                margin: 0 -10px;

                .dashboard-item-resources-wrapper {
                    float: left;
                    padding: 0 10px;
                    min-width: 190px;
                    max-width: 420px;
                    width: 50%;

                    .dashboard-item-resources {
                        // background: rgba( #d8d8d8, 0.1 );
                        // border: 1px solid rgba( #303c46, 0.1 );
                        // border-radius: 4px;
                        width: 100%;
                        height: 116px;
                        padding: 16px 20px;
                        color: $deepGray;

                        &.warning {
                            // border-color: #f7ba2a;
                            // background: rgba( #f7ba2a, 0.37 );
                        }

                        &.danger {
                            border-color: rgba( #FF4949, 0.37 );
                            background: rgba( #FF4949, 0.1 );
                        }

                        .resources-name {
                            color: #222222;
                            margin-bottom: 6px;

                            >i {
                                color: #808b91;
                            }
                        }

                        .resources-statistics {
                            font-weight: 400;
                            font-size: $fontSizeLarge;

                            .resources-used, .resources-count {
                                font-size: 30px;
                                color: #222222
                            }
                        }

                        .progress-bar {
                            width: 100%;
                            max-width: 220px;
                            height: 24px;
                            position: relative;
                            padding: 10px 0;

                            .progress-bar-container {
                                margin-right: 50px;
                                height: 6px;
                                border-radius: 3px;
                                background-color: #d4d8db; 

                                .progress-bar-content {
                                    height: 100%;
                                    background-color: $primary;
                                    border-radius: 3px;
                                }
                            }

                            .progress-bar-text {
                                font-size: 12px;
                                color: $lightGray;
                                position: absolute;
                                left: 100%;
                                margin-left: -44px;
                                width: 70px;
                                top: 5px;
                            }
                        }
                        
                    }
                }
            }

            .top5-empty {
                text-align: center;
                padding-top: 60px;
                width: 100%;
                @include user-select(none);

                .no-data-tip {
                    margin-top: 16px;
                    font-size: 14px;
                    color: $gray;
                }
            }

            .top5-list {
                width: 100%;
                position: relative;

                .top5-li {
                    height: 50px;
                    font-size: 14px;
                    border-bottom: 1px solid $bdGray;
                    text-align: center;
                    line-height: 48px;
                    color: $deepGray;
                    position: relative;

                    >div {
                        padding: 0 8px;
                        text-align: left;
                        @include text-overflow();
                        overflow: hidden;
                    }
                    .top5-no {
                        position: absolute;
                        left: 0;
                        width: 80px;
                        height: 100%;
                    }

                    .top5-title {
                        width: 80%;
                        float: left;
                        padding-left: 88px;
                        height: 100%;

                        .resource-img-container {
                            float: left;
                            height: 42px;
                            width: 42px;
                            margin: 4px 8px 4px 0;
                            overflow: hidden;
                            border-radius: 50%;
                            position: relative;

                            img {
                                height: 42px;
                                position: absolute;
                            }
                        }
                        

                        a {
                            float: left;
                        }

                        a:hover {
                            color: $info;
                        }
                    }

                    .top5-visit {
                        width: 20%;
                        float: left;
                        text-align: right;
                    }

                }

                .top5-li.header {
                    color: $gray;
                }
            }
        }
    }
}
</style>
